package com.feng.behavior
{
	import com.feng.ControllerCenter;
	import com.feng.debug.logger;
	import com.feng.model.EnmityRecord;
	import com.feng.model.Player;
	import com.feng.utils.ChallengeCompute;

	/**
	 * 攻击行为
	 * @author 风之守望者
	 * 2013-3-27
	 */
	public class AttackBehavior extends BehaviorBase
	{
		/**攻击欲望 表	值越大攻击欲望越大 */
		public var attckDesireTable:Array;

		public function AttackBehavior(controllerCenter:ControllerCenter)
		{
			super(controllerCenter);
		}

		/**
		 * 增加攻击欲望
		 * @param playerId 目标玩家编号
		 * @param value 攻击欲望 值
		 */
		public function addAttckDesire(playerId:int, value:int):void
		{
			if (value == 0)
				return;
			logger("对玩家" + playerId + "增加仇恨" + value);
			attckDesireTable[playerId] += value;
		}

		/** 抢人头	有便宜一定要捡啊！ */
		public function kill_steal(priority:Number):void
		{
			//与我同时出手的玩家编号
			var playerIdList:Array = attackOrderAnalysis.getSameSpeed();

			var target:Player;
			var attacker:Player;
			var attackerId:int;
			for each (target in enemyList)
			{
				var myDamage:int = feng.attackDamage(target) * attackOrderAnalysis.canAttackNum;
				if (target.hp <= myDamage)
				{
					addAttckDesire(target.id, priority);
					return;
				}

				for each (attackerId in playerIdList)
				{
					attacker = playerDic[attackerId];
					if (attacker)
					{
						var damage:Number = attacker.attackDamage(target) * attackOrderAnalysis.canAttackNum;
						//判断是否能够击毙该玩家
						if (target.hp <= damage)
						{
							if (target.id != attacker.id)
							{
								addAttckDesire(target.id, priority);
							}
							else if (target.id == attacker.id && attacker.hp <= 15)
							{
								addAttckDesire(target.id, priority / 2);
							}
						}
					}
				}
			}

		}

		/** 复仇	谁砍我最多我就打他 */
		public function revenge(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				addAttckDesire(player.id, attackMeDic[player.id] * priority);
			}
		}

		/** 嫉妒血量多	寻找血量最多的攻击(看不得人好，嫉妒啊) */
		public function attack_max_hp(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				addAttckDesire(player.id, (player.hp - feng.hp) * priority);
			}
		}

		/** 嫉妒速度快	寻找速度最快的攻击(看不得人好，嫉妒啊) */
		public function attack_max_speed(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				addAttckDesire(player.id, (player.speed - feng.speed) * priority);
			}
		}

		/** 嫉妒攻击力最高 */
		public function attack_max_attack(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				addAttckDesire(player.id, (player.attack - feng.attack) * priority);
			}
		}

		public function attack_not_max_attack(priority:Number):void
		{
			//不攻击有利用价值的高攻击玩家
			addAttckDesire(multiplayerAnalysis.attackSortList[0].id, priority);
		}

		/** 欺凌弱小者	寻找血量最少的欺负 */
		public function attack_min_hp(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				addAttckDesire(player.id, (feng.hp - player.hp) * priority);
			}
		}

		/** 嫉妒单挑实力最强的	谁单挑最强我就打谁 */
		public function attack_max_strong(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				//单挑后剩余的血量 （如果为负数，则输了，该值为对方剩余的血量）
				ChallengeCompute.maxDepth = ChallengeCompute.GENERAL_MAX_DEPTH;
				var residualHP:Number = ChallengeCompute.challenge(feng, player);
				addAttckDesire(player.id, -1 * residualHP * priority);
			}
		}

		public function attack_min_strong(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				//单挑后剩余的血量 （如果为负数，则输了，该值为对方剩余的血量）
				ChallengeCompute.maxDepth = ChallengeCompute.GENERAL_MAX_DEPTH;
				var residualHP:Number = ChallengeCompute.challenge(feng, player);
				addAttckDesire(player.id, residualHP * priority);
			}
		}

		public function attack_max_damage(priority:Number):void
		{
			var player:Player;
			for each (player in enemyList)
			{
				var damage:Number = feng.attackDamage(player);
				addAttckDesire(player.id, damage * priority);
			}
		}

		public function attack_max_enmity(priority:Number):void
		{
			var player:Player;
			var enmityRecord:EnmityRecord;
			var maxPreTurnAttackNum:int = 0;

			for each (player in enemyList)
			{
				enmityRecord = stateData.enmityRecordDic[player.id];
				if (maxPreTurnAttackNum < enmityRecord.preTurnAttackNum)
				{
					maxPreTurnAttackNum = enmityRecord.preTurnAttackNum;
				}
			}
			for each (player in enemyList)
			{
				enmityRecord = stateData.enmityRecordDic[player.id];
				addAttckDesire(player.id, priority * enmityRecord.preTurnAttackNum / maxPreTurnAttackNum);
			}
		}

		public function attack_max_killsteal(priority:Number):void
		{
			for (var i:int = 0; i < killStealAnalysis.killStealProbabilityList[0].length; i++)
			{
				if (killStealAnalysis.killStealProbabilityList[0].player.id != feng.id)
				{
					addAttckDesire(killStealAnalysis.killStealProbabilityList[0].player.id, priority);
					break;
				}
			}
		}

		public function attack_easy_losthead(priority:Number):void
		{
			for (var i:int = 0; i < killStealAnalysis.lostHeadProbabilityList.length; i++)
			{
				if (killStealAnalysis.lostHeadProbabilityList[i].player.id != feng.id)
				{
					addAttckDesire(killStealAnalysis.lostHeadProbabilityList[i].player.id, priority);
					break;
				}
			}

			//不攻击有利用价值的高攻击玩家
			addAttckDesire(multiplayerAnalysis.attackSortList[0].id, priority);
			
		}

		public function attack_freeride(priority:Number):void
		{
			for (var i:int = 0; i < killStealAnalysis.lostHeadProbabilityList.length; i++)
			{
				if (killStealAnalysis.lostHeadProbabilityList[i].player.id != feng.id)
				{
					addAttckDesire(killStealAnalysis.lostHeadProbabilityList[i].player.id, priority);
					break;
				}
			}
			
		}

		public function attack_killsteal_enmity(priority:Number):void
		{
			for (var i:int = 0; i < killStealAnalysis.killStealProbabilityList.length; i++)
			{
				var killStealId:int = killStealAnalysis.killStealProbabilityList[i].player.id;
				if (stateData.enmityRecordDic[killStealId].preTurnAttackNum > 0 && killStealId != feng.id)
				{
					addAttckDesire(killStealId, priority);
					break;
				}
			}
		}

		public function attack_not_die(priority:Number):void
		{
			var maxspeed:int = multiplayerAnalysis.speedSortList[0].speed;
			var i:int = 0;
			var j:int = 0;
			for (i = 0; i < enemyList.length; i++)
			{
				var target:Player = enemyList[i].copy();
				target.hp = target.hp - feng.attackDamage(target);
				for (j = 0; j < enemyList.length; j++)
				{
					var attacker:Player = enemyList[j];
					if (target.id != attacker.id)
					{
						var damage:int = attacker.attackDamage(target) + 4;
						if (attacker.speed == maxspeed)
						{
							damage = damage * 2;
						}
						if (target.hp <= damage)
						{
							addAttckDesire(target.id, priority);
							break;
						}

					}
				}
			}
		}

		public function attack_not_continuation(priority:Number):void
		{
			for (var i:int = stateData.attackPlayerIdList.length - 1; i >= 0; i--)
			{
				if (stateData.attackPlayerIdList[i].turnIndex == stateData.turnIndex - 1)
				{
					addAttckDesire(stateData.attackPlayerIdList[i].targetId, priority);
				}
				if (stateData.attackPlayerIdList[i].turnIndex < stateData.turnIndex - 1)
					break;
			}
		}

		/**
		 *  三人模式攻击
		 */
		public function attact_three_strategy(priority:Number):void
		{
			/*
			switch (threeStateAnalysis.myState)
			{
				//最强
				case ThreeAnalysis.FORTISSIMO:

					break;
				//最弱
				case ThreeAnalysis.WEAKEST:
					//
					addAttckDesire(threeStateAnalysis.punyEnemy.id, priority);
					break;
				//中间(比强的弱，比弱的强)
				case ThreeAnalysis.MIDDLE:
					addAttckDesire(threeStateAnalysis.punyEnemy.id, priority);
					break;
				//牵制(比强还强，比弱还弱)
				case ThreeAnalysis.CONTAIN:

					break;
				default:

					break;
			}
			*/
			var i:int;
			var j:int;
			for (i = 0; i < killStealAnalysis.lostHeadProbabilityList.length; i++)
			{
				if (killStealAnalysis.lostHeadProbabilityList[i].player.id != feng.id)
				{
					for (j = 0; j < killStealAnalysis.killStealProbabilityList.length; j++)
					{
						if (killStealAnalysis.lostHeadProbabilityList[i].player.id != killStealAnalysis.killStealProbabilityList[j].player.id)
						{
							if (killStealAnalysis.killStealProbabilityList[j].player.id == feng.id)
							{
								addAttckDesire(killStealAnalysis.lostHeadProbabilityList[i].player.id, priority);
							}
							break;
						}
					}
				}
			}
		}

		/** 四人模式攻击 */
		public function attact_four_strategy(priority:Number):void
		{

		}

		/** 五人模式攻击 */
		public function attact_five_strategy(priority:Number):void
		{

		}

		/** 六人模式攻击 */
		public function attact_six_strategy(priority:Number):void
		{


		}

	}
}
